﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLib;
using System.Windows.Controls;
using Sandbox.ContentTypes;
using Sandbox.Names;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using GameLib.Utils;
using GameLib.WorldMap;
using System.Windows;
using System.Windows.Media.Effects;

namespace Sandbox.Scene
{
	public class TileSceneObject
	{
		public TileSceneObject(MapScene scene, Tile tile)
		{
			m_Scene = scene;
			m_Tile = tile;
			m_Tile.TerrainChanged += new EventHandler(Tile_TerrainChanged);
		}


		public void Display()
		{
			/*
			foreach (Entity entity in m_Tile.Entities)
			{
				Image entityImage = new Image();
				EntityVisual visual = entity.Type.Content.Get<EntityVisual>(ContentNames.EntityVisual);

				if (visual != null)
					entityImage.Source = visual.GetImageSource(entity);

				m_Scene.MapCanvas.Children.Add(entityImage);
			}*/

			if (m_TileImageDrawing == null)
			{
				CreateTileImageDrawing();
			}

			if (m_TileImageDrawing != null)
				m_Scene.TerrainDrawingGroup.Children.Add(m_TileImageDrawing);

			if (m_TileOverlayDrawing != null)
				m_Scene.OverlayDrawingGroup.Children.Add(m_TileOverlayDrawing);

			

			if (m_Tile.Settlement != null)
			{
				m_Scene.OverlayDrawingGroup.Children.Add(m_TileSettlementDrawing);


				if (NameText == null)
				{
					NameText = new TextBlock();
					NameText.Text = m_Tile.Settlement.Name;
					NameText.Foreground = new SolidColorBrush(Color.FromRgb(255, 70, 80));
					DropShadowEffect effect = new DropShadowEffect();
					effect.BlurRadius = 3;
					effect.ShadowDepth = 2;
					effect.Color = Colors.Black;
					NameText.Effect = effect;
					NameText.FontSize = 30;
					NameText.FontWeight = FontWeights.Black;
					//NameText.FontStyle = FontStyles.Italic;
					NameText.FontFamily = new FontFamily("KaiTi");
					Point center = m_Scene.GetTileCenter(m_Tile.X, m_Tile.Y);
					NameText.Measure(new Size(100000000000000, 100000000000000000000.0));
					NameText.SetValue(Canvas.LeftProperty, center.X - NameText.DesiredSize.Width / 2);
					NameText.SetValue(Canvas.TopProperty, center.Y + m_Scene.HexHeight / 4);
					m_Scene.GetSceneObjectCanvas(MapSceneObjectLayers.AboveGrids).Children.Add(NameText);
				}
			}
		}

		void Tile_TerrainChanged(object sender, EventArgs e)
		{
			if (m_TileImageDrawing != null)
			{
				m_TileImageDrawing = null;
				m_Scene.OnTileContentChanged(this);
			}
		}

		protected void CreateTileImageDrawing()
		{
			//string[] names = { "grassland", "plain", "forest_light", "forest_dense", "swamp", "desert", "hill", "mountain", "water" };
			//string picked_name = names[rand.Next(names.Length)];

			BitmapImage bitmap = FindTileTerrainImage();

			//Image image = new Image();
			//image.Source = bitmap;
			//image.Width = bitmap.PixelWidth;
			//image.Height = bitmap.PixelHeight;

			double left = m_Scene.HexWidth * 3 / 4 * m_Tile.X -m_Scene.HexWidth / 4;
			double top = m_Scene.HexHeight / 2 * (m_Tile.Y - 1); // (m_Tile.X % 2 == 0 ? m_Scene.HexHeight / 2 : 0);

			//image.SetValue(Canvas.LeftProperty, left);
			//image.SetValue(Canvas.TopProperty, top);

			//m_Scene.MapCanvas.Children.Add(image);
			if (bitmap != null)
			{
				m_TileImageDrawing = new ImageDrawing();
				m_TileImageDrawing.ImageSource = bitmap;
				m_TileImageDrawing.Rect = new System.Windows.Rect(left, top, bitmap.PixelWidth, bitmap.PixelHeight);
			}
			else
			{
				m_TileImageDrawing = null;
			}

			
			BitmapImage overlay = FindTileOverlayImage();

			if (overlay != null)
			{
				m_TileOverlayDrawing = new ImageDrawing();
				m_TileOverlayDrawing.ImageSource = overlay;
				m_TileOverlayDrawing.Rect = new System.Windows.Rect(left, top, overlay.PixelWidth, overlay.PixelHeight);
			}
			else
			{
				m_TileOverlayDrawing = null;
			}

			m_TileSettlementDrawing = new ImageDrawing();
			BitmapImage settlement = new BitmapImage(new Uri("../../Content/Images/Map/Elements/city.png", UriKind.Relative));
			m_TileSettlementDrawing.ImageSource = settlement;
			m_TileSettlementDrawing.Rect = new System.Windows.Rect(left, top, settlement.PixelWidth, settlement.PixelHeight);

		}

		protected BitmapImage FindTileTerrainImage()
		{
			StaticImage image = m_Tile.Terrain.Content.Get<StaticImage>(Name.FromString("TileImage"));

			if (image != null)
				return image.Source;

			return null; //new BitmapImage(new Uri("Content/Images/Map/Terrains/" + picked_name + ".png", UriKind.Relative));
		}

		protected BitmapImage FindTileOverlayImage()
		{
			StaticImage image = m_Tile.Terrain.Content.Get<StaticImage>(Name.FromString("OverlayImage"));

			if (image != null)
				return image.Source;

			return null;
		}

		private static Random rand = new Random();

		private MapScene m_Scene;
		private Tile m_Tile;
		private ImageDrawing m_TileImageDrawing;
		private ImageDrawing m_TileOverlayDrawing;
		private ImageDrawing m_TileSettlementDrawing;
		private TextBlock NameText;
	}
}
